<template>
  <div>
    <Row>
      <i-col>
        <Table :columns="containerColumn" :data="containerData"></Table>
      </i-col>
    </Row>
    <br/>
    <Button type="primary" @click="exportDataInventory" style="margin-left: 10px;">
      <Icon type="ios-download-outline"></Icon>
      导出文件
    </Button>
  </div>
</template>
<script>
  import tablePage from '@components/table/tablePage.vue'
  import { getContainerHis } from '@api/containerPlatform/containerManagement'
  import { exportUtil } from '@/vendor/exportInvoke'

  export default {
    name: 'trackingCode',
    data () {
      let tabParam = {
        containerNo: ''
      }
      let containerColumn = [
        {
          title: '发站',
          key: 'sendStationName',
          width: 80
        },
        {
          title: '发出时间',
          key: 'startStationTime',
          width: 100
        },
        {
          title: '到站',
          key: 'endStationName',
          width: 80
        },
        {
          title: '到达时间',
          key: 'endStationTime',
          width: 100
        },
        {
          title: '发货人',
          key: 'sender',
          width: 210
        },
        {
          title: '收货人',
          key: 'receiver',
          width: 210
        },
        {
          title: '状态',
          key: 'isEmpty',
          width: 60,
          render: (h, params) => {
            let thisdata = params.row.isEmpty === 1 ? '重' : '空'
            return (<span>{thisdata}</span>)
          }
        },
        {
          title: '用时',
          key: 'useTime',
          width: 100,
          render: (h, params) => {
            return (<span>{Number(params.row.useTime).toFixed(2)}小时</span>)
          }
        }
      ]
      let containerData = []
      let datas = { getContainerHis, tabParam, containerColumn, containerData }
      return datas
    },
    props: {
      containerNo: ''
    },
    methods: {
      exportDataInventory () {
        let param = Object.assign({}, this.tabParam)
        param.containerNo = this.containerNo
        getContainerHis(this.tabParam).then(res => {
          if (res.data.isSuccess === 1) {
            console.log(res)
            let resDate = res.data.data
            let result = this.fasetJson(resDate)
            exportUtil.export2Excel('箱历史报表' + this.containerNo, result.columns, result.data)
          }
        })
      },
      fasetJson (tabData) {
        let result = {
          columns: [
            { title: '发站', key: 'fzmc' },
            { title: '发出时间', key: 'fcsj' },
            { title: '到站', key: 'dzmc' },
            { title: '到达时间', key: 'ddsj' },
            { title: '发货人', key: 'fhr' },
            { title: '收货人', key: 'shr' },
            { title: '状态', key: 'kzbz' },
            { title: '用时', key: 'useTime' }
          ],
          data: []
        }
        let indexCount = 0
        tabData.forEach((item) => {
          let dataItem = {
            fzmc: item.fzmc,
            fcsj: item.fcsj,
            dzmc: item.dzmc,
            ddsj: item.ddsj,
            fhr: item.fhr,
            shr: item.shr,
            kzbz: item.kzbz === 1 ? '重' : '空',
            useTime: item.useTime + '小时'
          }
          result.data.push(dataItem)
          indexCount++
        })
        console.log(indexCount)
        return result
      }
    },
    mounted () {
      this.tabParam.containerNo = this.containerNo
      getContainerHis(this.tabParam)
        .then(res => {
          if (+res.data.isSuccess === 1) {
            this.containerData = res.data.data
          }
        })
    },
    components: { tablePage }
  }
</script>
<style>

</style>
